#include "SpriteStatebar.h"
#include "CGlobal.h"
#include "ConstDefine.h"
#include "GameLayer.h"
using namespace cocos2d;
SpriteStatebar::SpriteStatebar(void)
{
	nSafecapNum_=0;
	nArmorNum_=0;
	nGhostNum_=0;
	lableArray_=NULL;
	pGamePlayer_=NULL;
}

SpriteStatebar::~SpriteStatebar(void)
{
	if (lableArray_)
	{
		delete []lableArray_;
		lableArray_=NULL;
	}
}

SpriteStatebar * SpriteStatebar::node(GameLayer *pLayer)
{
	SpriteStatebar *pState=new SpriteStatebar;
	if (pState)
	{
		pState->autorelease();
		if (pState->init(pLayer))
		{
			return pState;
		}
	}
	return NULL;
}

bool SpriteStatebar::init(GameLayer *pLayer)
{
	if (!CCSprite::init())
	{
		return false;
	}
	
	lableArray_=new cocos2d::CCArray;
	if (!addBar())
	{
		return false;
	}
	pGamePlayer_=pLayer;
	
	return true;
}

void SpriteStatebar::addSafecap( int nAdd )
{
	nSafecapNum_+=nAdd;
	setLableString(BAR_SAFECAP,nSafecapNum_);
}

void SpriteStatebar::addArmorNum( int nAdd )
{
	nArmorNum_+=nAdd;
	setLableString(BAR_ARMOR,nArmorNum_);
}

void SpriteStatebar::addGhostNum( int nAdd )
{
	nGhostNum_+=nAdd;
	setLableString(BAR_GHOST,nGhostNum_);
}

bool SpriteStatebar::addBar()
{

	{
		std::string strArray[]={"safecap.png","safecap.png","safecap.png"};
		int nArrayNum=sizeof(strArray)/sizeof(std::string);
		addItem(strArray,nArrayNum,nSafecapNum_,BAR_SAFECAP,menu_selector(SpriteStatebar::menuSafecap));
		
	}
	{
		std::string strArray[]={"armor.png","armor.png","armor.png"};
		int nArrayNum=sizeof(strArray)/sizeof(std::string);
		addItem(strArray,nArrayNum,nArmorNum_,BAR_ARMOR,menu_selector(SpriteStatebar::menuArmor));
	}
	{
		std::string strArray[]={"ghost.png","ghost.png","ghost.png"};
		int nArrayNum=sizeof(strArray)/sizeof(std::string);
		addItem(strArray,nArrayNum,nGhostNum_,BAR_GHOST,menu_selector(SpriteStatebar::menuGhost));
	}
	return true;
	
}

void SpriteStatebar::menuSafecap(cocos2d::CCObject *pObj)
{
	if (nSafecapNum_<=0)
	{
		return;
	}
	if (pGamePlayer_)
	{
		if (pGamePlayer_->safecapClick())
		{
			nSafecapNum_--;
			setLableString(BAR_SAFECAP,nSafecapNum_);
		}
	}
}

void SpriteStatebar::menuArmor(cocos2d::CCObject *pObj)
{
	if (nArmorNum_<=0)
	{
		return;
	}
	
	if (pGamePlayer_)
	{
		if (pGamePlayer_->armorClick())
		{
			nArmorNum_--;
			setLableString(BAR_ARMOR,nArmorNum_);
		}
	}
}

void SpriteStatebar::menuGhost(cocos2d::CCObject *pObj)
{
	if (nGhostNum_<=0)
	{
		return;
	}
	
	if (pGamePlayer_)
	{
		if (pGamePlayer_->ghostClick())
		{
			nGhostNum_--;
			setLableString(BAR_GHOST,nGhostNum_);
		}
	}
}





void SpriteStatebar::addItem( const std::string strArray[],int nArrayNum,int nValue,BAR_TAG barTag,const SEL_MenuHandler &menuHandle)
{
	
	std::vector<CCSprite *> vecBtn;

	for (int i=0;i<nArrayNum;i++)
	{
		CCSprite *pPic=CGlobal::SpriteWithFile(strArray[i].c_str());
		CCLabelTTF *pLable=CCLabelTTF::labelWithString("x 0",CCSize(0,0),kCCTextAlignmentLeft,"arial",40);
		if (pPic&&pLable)
		{
			
			pPic->setScale(0.6);
			pPic->addChild(pLable);
			pLable->setTag(barTag);
			pLable->setPosition(ccp(150,50));
			lableArray_->addObject((CCObject*)pLable);
			vecBtn.push_back(pPic);
		}	
	}
	CCSprite *btn[3];
	for (int i=0;i<3;i++)
	{
		btn[i]=NULL;
	}
	int nMinNum=vecBtn.size()>3?3:vecBtn.size();
	for (int i=0;i<nMinNum;i++)
	{
		btn[i]=vecBtn[i];
	}
	
	CCMenuItemSprite *pItem=CCMenuItemSprite::itemWithNormalSprite(btn[0],btn[1],btn[2],this,menuHandle);
	CCMenu *pMenu=(CCMenu*) getChildByTag(TAG_MENU);
	if (!pMenu)
	{
		pMenu=CCMenu::node();
		if (pMenu)
		{
			addChild(pMenu,0,TAG_MENU);
			pMenu->setPosition(ccp(0,0));
		}
	}
	if (pMenu)
	{
		//pMenu->menuWithItem(pItem);
		pItem->setPosition(ccp(200.0f*pMenu->getChildrenCount(),0));
		pMenu->addChild(pItem);
	}
	
}

void SpriteStatebar::setLableString(BAR_TAG barTag,int nValue) 
{
	CCObject *pObj=NULL;
	CCARRAY_FOREACH(lableArray_,pObj)
	{
		CCLabelTTF *pLable=(CCLabelTTF*)pObj;
		if (pLable->getTag()==barTag)
		{
			char buf[50]={0};
			sprintf(buf,"x %d",nValue);
			pLable->setString(buf);
		}
	}
}